﻿/*****
 *  Copyright 2011, Kaven Gagné
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.ComponentModel;

namespace Knowledge
{
    /// <summary>
    /// Interaction logic for AddArticleWindow.xaml
    /// </summary>
    public partial class AddArticleWindow : Window, INotifyPropertyChanged, IDataErrorInfo
    {
        #region Constructors
        
        // Dialog to add an Article
        public AddArticleWindow(string parentN) : this(parentN, false){}
        public AddArticleWindow(string parentN, bool isFolder)
        {
            InitializeComponent();
            this.DataContext = this;
            // Char to detect this is not edited
            ArticleName = "▲"; // ALT + 286
            parentName.Content = parentN;
            string itemType = (isFolder) ? "Folder" : "Article";
            this.Title = "New " + itemType;
            articleLabel.Content = itemType + " Name";
        }

        #endregion //Constructors




        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Adjust Window Position
            if (this.Owner.WindowState == WindowState.Maximized)
            {
                this.Left = 25;
                this.Top = 75;
            }
            else
            {
                this.Left = this.Owner.Left + 25;
                this.Top = this.Owner.Top + 75;
            }
        }



        private string _articleName;
        public string ArticleName
        {
            get
            {
                return _articleName;
            }
            set
            {
                _articleName = value;
                this.OnPropertyChanged("ArticleName");
            }
        }





        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            // Dialog box canceled
            this.DialogResult = false;
        }

        void okButton_Click(object sender, RoutedEventArgs e)
        {
            // Don't accept the dialog box if there is invalid data
            if (this.Errors.Count() > 0) return;
            if (string.IsNullOrEmpty(this.ArticleName)) return;

            // Dialog box accepted
            this.DialogResult = true;
        }






        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        protected Dictionary<string, string> _errors = new Dictionary<string, string>();
        public IDictionary<string, string> Errors
        {
            get { return _errors; }
        }


        private int MinLength = 1;
        public string this[string propertyName]
        {
            get
            {
                string errorMessage = string.Empty;
                this.Errors.Remove(propertyName);

                switch (propertyName)
                {
                    case "ArticleName":
                    {
                        if (this.ArticleName == "▲")
                        {
                            this.ArticleName = "";
                        }
                        else if (string.IsNullOrEmpty(this.ArticleName) || this.ArticleName.Trim().Length < MinLength)
                        {
                            errorMessage = string.Format("Name is a mandatory field.", MinLength);
                        }
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    this.Errors.Add(propertyName, errorMessage);
                }

                okButton.IsEnabled = (this.Errors.Count() < 1) && (this.ArticleName != "▲");

                return errorMessage;
            }
        }
        #endregion // IDataErrorInfo Members



        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion // INotifyPropertyChanged Members
    }
}
